package ias.springnote.db;

import ias.springnote.rest.ResourceException;
import javax.xml.stream.XMLStreamException;
import static org.hamcrest.Matchers.greaterThan;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

import jdave.Specification;
import jdave.junit4.JDaveRunner;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.runner.RunWith;


/**
 *
 * @author ias
 */
@RunWith(JDaveRunner.class)
public class DatabaseSpec extends Specification<DatabaseSpec> {

  public class SimpleDatabase {

    Database database;

    public Database create() {
      Properties properties = new Properties();
      try {
        properties.loadFromXML(new FileInputStream("springnote.xml"));
      } catch (InvalidPropertiesFormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      String userId = properties.getProperty("userId");
      String userKey = properties.getProperty("userKey");
      String appKey = properties.getProperty("appKey");
      String domain = properties.getProperty("domain");
      String root = properties.getProperty("root");
      Session s = new Session(userId, userKey, appKey);
      database = s.getDatabase(domain);
      database.setRoot(Integer.parseInt(root));
      return database;
    }

    public void createAndSave() throws ResourceException, XMLStreamException, JSONException {
      Document doc = new Document();
      JSONObject object = doc.getObject();
      object.put("c", "e");
      database.saveDocument(doc);
      doc = database.getDocument(object.getInt("id"));
      System.out.println(doc.getObject());
    }
  }
}
